// 设置cookie expires储存时间按秒计算
function setUserCookie(name, value, expires) {
    var date = new Date();
    date.setTime(date.getTime() + (expires * 1000));
    document.cookie = name + "="+ escape (value) + ";expires=" + date.toGMTString()+ ";path=/";
}

// 获取cookie'.
function getUserCookie(name) {
    var strcookie = document.cookie;//获取cookie字符串
    var arrcookie = strcookie.split("; ");//分割
    //遍历匹配
    for ( var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] === name){
            return arr[1];
        }
    }
    return "";
}

//清除cookie

function clearUserCookie(name) {
    setUserCookie(name, "", -1);
}


/**
 * 判断是否登陆
 */
function isLogin(){
    var token = getUserCookie("token");
    if(token !== null && token !== undefined && "" !== token && "undefined" !== token){
        return true;
    }
    return false;
}


var defAvatarImg = "";
function getUserInfo(){
    var userInfoStr = localStorage.getItem("userInfo");
    if(null == userInfoStr){
        return null;
    }
    var userInfo = JSON.parse(userInfoStr);
    if(null == userInfo.avatar ||"" === userInfo.avatar){
        userInfo.avatar = defAvatarImg
    }

    return userInfo;
}

/**
 * 处理 登陆按钮
 */
function handleLoginBtn(){
    if(!isLogin()){
        return;
    }

    var userInfo = getUserInfo();
    if(null == userInfo){
        return;
    }

    $("#login-btn>.login-btn-text").text(userInfo.nickname);
    $("#login-btn>.avatar").show();
    $("#login-btn>.avatar>image").attr("src", userInfo.avatar).show();
    $("#login-btn").attr("onclick", "open_user_info()");
    $("#login-btn>.login-ico").hide();
}
